package com.fcc.wfly.common.domain;

import org.springframework.util.Assert;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ErrorHandleConst {
	public static final String PN_NOT_EMPTY = "The pn cannot be empty";
	public static final String SN_NOT_EMPTY = "The sn cannot be empty";
	public static final String PN_OVER_LIMIT = "The length of pn cannot over 10";
	public static final String SN_OVER_LIMIT = "The length of sn cannot over 20";
	public static final String DATE_NOT_EMPTY = "The date cannot be empty";
	public static final String DURING_TIME = "The days between start date and end date should be less than or equal to 60 days";
	public static final String DATE_ERROR_FORMAT = "The Date format should be like yyyy-MM-dd'T'HH:mm:ss'Z'";
	public static final String EVENT_CODE_NOT_EMPTY = "The event_code cannot be empty";
	public static final String EVENT_CODE_OVER_LIMIT = "The length of event_code cannot over 20";
	public static final String EVENT_CODE = "eventCode";
	public static final String DETECTION_DATE = "detectionDate";
	public static final String SOLUTIONID_NOT_EMPTY = "The solutionID cannot be empty";
	public static final String FEEDBACK_NOT_EMPTY = "The feedback cannot be empty";
	public static final String SOLUTIONID_OVER_LIMIT = "The length of soluntionID cannot over 100";
	public static final String FEEDBACK_OVER_LIMIT = "The feedback of feedback cannot over 10000";
	public static final String SCORE_NOT_EMPTY = "The score cannot be empty";
	public static final String SCORE_NOT_INT = "The score must be positive integer";

	public static final String SCORE_LIMIT = "The value of score must over 0 and less than or equal to 10";


	
	protected static SimpleDateFormat DATEFORMAT_REQUEST = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	private static int diff;
	
	public static void ParamsValidate(String pn, String sn, String startDate, String endDate){
		try {
			Date startDate1 = DATEFORMAT_REQUEST.parse(startDate);
			Date startDate2 = DATEFORMAT_REQUEST.parse(endDate);
			diff = ErrorHandleConst.differentDaysByMillisecond(startDate1,startDate2);
		} catch (Exception e) {
			throw new RuntimeException(ErrorHandleConst.DATE_ERROR_FORMAT);
		}
		Assert.hasText(pn, ErrorHandleConst.PN_NOT_EMPTY);
		Assert.hasText(sn, ErrorHandleConst.SN_NOT_EMPTY);
		Assert.isTrue(pn.length()<=10, ErrorHandleConst.PN_OVER_LIMIT);
		Assert.isTrue(sn.length()<=20, ErrorHandleConst.SN_OVER_LIMIT);
		Assert.hasText(startDate, ErrorHandleConst.DATE_NOT_EMPTY);
		Assert.hasText(endDate, ErrorHandleConst.DATE_NOT_EMPTY);
		Assert.isTrue(diff<=60, ErrorHandleConst.DURING_TIME);	
	}
	
	public static void SolutionParamsValidate(String pn, String sn, String eventCode, String detectionDate){
		try {
			Date Date = DATEFORMAT_REQUEST.parse(detectionDate);
		} catch (Exception e) {
			throw new RuntimeException(ErrorHandleConst.DATE_ERROR_FORMAT);
		}
		Assert.hasText(pn, ErrorHandleConst.PN_NOT_EMPTY);
		Assert.hasText(sn, ErrorHandleConst.SN_NOT_EMPTY);
		Assert.isTrue(pn.length()<=10, ErrorHandleConst.PN_OVER_LIMIT);
		Assert.isTrue(sn.length()<=20, ErrorHandleConst.SN_OVER_LIMIT);
		Assert.hasText(eventCode, ErrorHandleConst.EVENT_CODE_NOT_EMPTY);
		Assert.isTrue(eventCode.length()<=20, ErrorHandleConst.EVENT_CODE_OVER_LIMIT);
	}
	
	public static void MaintenanceTasksParamsValidate(String pn, String sn, String date) {
		try {
			Date Date = DATEFORMAT_REQUEST.parse(date);
			System.out.println(Date);
		} catch (Exception e) {
			throw new RuntimeException(ErrorHandleConst.DATE_ERROR_FORMAT);
		}
		Assert.hasText(pn, ErrorHandleConst.PN_NOT_EMPTY);
		Assert.hasText(sn, ErrorHandleConst.SN_NOT_EMPTY);
		Assert.isTrue(pn.length()<=10, ErrorHandleConst.PN_OVER_LIMIT);
		Assert.isTrue(sn.length()<=20, ErrorHandleConst.SN_OVER_LIMIT);	
		Assert.hasText(date, ErrorHandleConst.DATE_NOT_EMPTY);
	}

	public static void PnSnParamsValidate(String pn, String sn) {
		Assert.hasText(pn, ErrorHandleConst.PN_NOT_EMPTY);
		Assert.hasText(sn, ErrorHandleConst.SN_NOT_EMPTY);
		Assert.isTrue(pn.length()<=10, ErrorHandleConst.PN_OVER_LIMIT);
		Assert.isTrue(sn.length()<=20, ErrorHandleConst.SN_OVER_LIMIT);	
	}

	public static boolean PostSolutionParamsValidate(String pn, String sn, String eventCode, String date,
			String solutionId, String feedback, String score, HttpServletResponse response) {
		try {
			Date Date = DATEFORMAT_REQUEST.parse(date);
		} catch (Exception e) {
			throw new RuntimeException(ErrorHandleConst.DATE_ERROR_FORMAT);
		}
		Assert.hasText(pn, ErrorHandleConst.PN_NOT_EMPTY);
		Assert.hasText(sn, ErrorHandleConst.SN_NOT_EMPTY);
		Assert.isTrue(pn.length()<=10, ErrorHandleConst.PN_OVER_LIMIT);
		Assert.isTrue(sn.length()<=20, ErrorHandleConst.SN_OVER_LIMIT);
		Assert.hasText(eventCode, ErrorHandleConst.EVENT_CODE_NOT_EMPTY);
		Assert.isTrue(eventCode.length()<=20, ErrorHandleConst.EVENT_CODE_OVER_LIMIT);
		Assert.hasText(date, ErrorHandleConst.DATE_NOT_EMPTY);
		Assert.hasText(solutionId, ErrorHandleConst.SOLUTIONID_NOT_EMPTY);
		Assert.hasText(feedback, ErrorHandleConst.FEEDBACK_NOT_EMPTY);
		Assert.isTrue(solutionId.length()<=100, ErrorHandleConst.SOLUTIONID_OVER_LIMIT);
		Assert.isTrue(feedback.length()<=10000, ErrorHandleConst.FEEDBACK_OVER_LIMIT);
		Assert.hasText(score, ErrorHandleConst.SCORE_NOT_EMPTY);
		Assert.isTrue(score.matches("^[1-9]\\d*$"), ErrorHandleConst.SCORE_NOT_INT);
		Assert.isTrue(Integer.parseInt(score)<=10,ErrorHandleConst.SCORE_LIMIT);
		return true;
	}
	
	public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }

}

